﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AmkeniCrossLoginApplication.Data_Access;
using Microsoft.Reporting.WebForms;

namespace AmkeniCrossLoginApplication.Reports
{
    public partial class ReportCreator : System.Web.UI.Page
    {
        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        public void createNarrativeReport(int call, int year, int reportPeriod)
        {
            //project context
            //Gender Mainstreaming
            //Partnerships
            //Challenges
            //Lessons
            //Recommendations
            if (reportPeriod == 5)
            {
                var narrativeReport = from data in db.ip_report
                                      where data.RFP_id == call &&
                                      data.year_id == year
                                      select new
                                      {
                                          ip_name = data.implementing_partner.abbreviation,
                                          call = data.RFP.RFP_name,
                                          report_period = data.report_period.period,
                                          year = data.year.year1,
                                          data.project_context,
                                          data.gender_mainstreaming,
                                          data.partnerships,
                                          data.challenges,
                                          data.lessons,
                                          data.recommendations
                                      };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource narrativeDataSource = new ReportDataSource("NarrativeDataSet", narrativeReport);
                viewer.LocalReport.DataSources.Add(narrativeDataSource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/NarrativeReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in narrativeReport)
                {
                    fileName = "Narrative Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
            else
            {
                var narrativeReport = from data in db.ip_report
                                      where data.RFP_id == call &&
                                      data.year_id == year &&
                                      data.report_period_id == reportPeriod
                                      select new
                                      {
                                          ip_name = data.implementing_partner.abbreviation,
                                          call = data.RFP.RFP_name,
                                          report_period = data.report_period.period,
                                          year = data.year.year1,
                                          data.project_context,
                                          data.gender_mainstreaming,
                                          data.partnerships,
                                          data.challenges,
                                          data.lessons,
                                          data.recommendations
                                      };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource narrativeDataSource = new ReportDataSource("NarrativeDataSet", narrativeReport);
                viewer.LocalReport.DataSources.Add(narrativeDataSource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/NarrativeReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in narrativeReport)
                {
                    fileName = "Narrative Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
        }

        public void createResultsReport(int call, int year, int reportPeriod)
        {
            if (reportPeriod == 5)
            {
                var resultsReport = (from data in db.indicator_result
                                     where data.RFP_id == call &&
                                     data.year_id == year
                                     select new
                                     {
                                         call = data.ip_report.RFP.RFP_name,
                                         year = data.ip_report.year.year1,
                                         ip_name = data.ip_report.implementing_partner.abbreviation,
                                         report_period = data.ip_report.report_period.period,
                                         outcome = data.indicator.programme_output.programme_outcome.name,
                                         outcome_id = data.indicator.programme_output.programme_outcome.outcome_id,
                                         output = data.indicator.programme_output.name,
                                         output_id = data.indicator.programme_output.output_id,
                                         indicator = data.indicator.name,
                                         data.indicator.baseline,
                                         data.indicator.target,
                                         data.result,
                                         data.comment,
                                         data.status_id
                                     }).OrderBy(item => item.outcome_id);


                ReportViewer viewer = new ReportViewer();

                ReportDataSource resultsDataSource = new ReportDataSource("ResultsDataSet", resultsReport);
                viewer.LocalReport.DataSources.Add(resultsDataSource);

                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/ResultsReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in resultsReport)
                {
                    fileName = "Results Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
            else
            {
                var resultsReport = (from data in db.indicator_result
                                     where data.RFP_id == call &&
                                     data.year_id == year &&
                                     data.report_period_id == reportPeriod
                                     select new
                                     {
                                         call = data.ip_report.RFP.RFP_name,
                                         year = data.ip_report.year.year1,
                                         ip_name = data.ip_report.implementing_partner.abbreviation,
                                         report_period = data.ip_report.report_period.period,
                                         outcome = data.indicator.programme_output.programme_outcome.name,
                                         outcome_id = data.indicator.programme_output.programme_outcome.outcome_id,
                                         output = data.indicator.programme_output.name,
                                         output_id = data.indicator.programme_output.output_id,
                                         indicator = data.indicator.name,
                                         data.indicator.baseline,
                                         data.indicator.target,
                                         data.result,
                                         data.comment,
                                         data.status_id
                                     }).OrderBy(item => item.outcome_id);


                ReportViewer viewer = new ReportViewer();

                ReportDataSource resultsDataSource = new ReportDataSource("ResultsDataSet", resultsReport);
                viewer.LocalReport.DataSources.Add(resultsDataSource);

                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/ResultsReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in resultsReport)
                {
                    fileName = "Results Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
        }

        public void createM_and_EReport(int call, int year, int reportPeriod)
        {
            if (reportPeriod == 5)
            {
                var MandEReport = from data in db.monitoring_and_evaluation
                                  where data.RFP_id == call &&
                                  data.year_id == year
                                  select new
                                  {
                                      ip_name = data.ip_report.implementing_partner.abbreviation,
                                      call = data.ip_report.RFP.RFP_name,
                                      report_period = data.ip_report.report_period.period,
                                      year = data.ip_report.year.year1,
                                      data.activity,
                                      data.observation,
                                      data.recommendation,
                                      data.action_taken
                                  };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource MandEDataSource = new ReportDataSource("MandEDataSet", MandEReport);
                viewer.LocalReport.DataSources.Add(MandEDataSource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/MandEReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in MandEReport)
                {
                    fileName = "Monitoring and Evaluation Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
            else
            {
                var MandEReport = from data in db.monitoring_and_evaluation
                                  where data.RFP_id == call &&
                                  data.report_period_id == reportPeriod &&
                                  data.year_id == year
                                  select new
                                  {
                                      ip_name = data.ip_report.implementing_partner.abbreviation,
                                      call = data.ip_report.RFP.RFP_name,
                                      report_period = data.ip_report.report_period.period,
                                      year = data.ip_report.year.year1,
                                      data.activity,
                                      data.observation,
                                      data.recommendation,
                                      data.action_taken
                                  };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource MandEDataSource = new ReportDataSource("MandEDataSet", MandEReport);
                viewer.LocalReport.DataSources.Add(MandEDataSource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/MandEReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in MandEReport)
                {
                    fileName = "Monitoring and Evaluation Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
        }

        public void createSuccessStoryReport(int call, int year, int reportPeriod)
        {
            if (reportPeriod == 5)
            {
                var successStoryReport = from data in db.success_story
                                         where data.RFP_id == call &&
                                         data.year_id == year
                                         select new
                                         {
                                             ip_name = data.ip_report.implementing_partner.abbreviation,
                                             report_period = data.ip_report.report_period.period,
                                             call = data.ip_report.RFP.RFP_name,
                                             year = data.ip_report.year.year1,
                                             data.title,
                                             data.context,
                                             data.problem,
                                             data.intervention,
                                             data.result
                                         };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource successStoryDataSource = new ReportDataSource("SuccessStoryDataSet", successStoryReport);
                viewer.LocalReport.DataSources.Add(successStoryDataSource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/SuccessStoryReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in successStoryReport)
                {
                    fileName = "Success Story Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
            else
            {
                var successStoryReport = from data in db.success_story
                                         where data.RFP_id == call &&
                                         data.year_id == year &&
                                         data.report_period_id == reportPeriod
                                         select new
                                         {
                                             ip_name = data.ip_report.implementing_partner.abbreviation,
                                             report_period = data.ip_report.report_period.period,
                                             call = data.ip_report.RFP.RFP_name,
                                             year = data.ip_report.year.year1,
                                             data.title,
                                             data.context,
                                             data.problem,
                                             data.intervention,
                                             data.result
                                         };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource successStoryDataSource = new ReportDataSource("SuccessStoryDataSet", successStoryReport);
                viewer.LocalReport.DataSources.Add(successStoryDataSource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/SuccessStoryReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in successStoryReport)
                {
                    fileName = "Success Story Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
        }

        public void createUpcomingActivitiesReport(int call, int year, int reportPeriod)
        {
            if (reportPeriod == 5)
            {
                var upcomingActivitiesReport = from data in db.upcoming_activities
                                               where data.RFP_id == call &&
                                               data.year_id == year
                                               select new
                                               {
                                                   ip_name = data.ip_report.implementing_partner.abbreviation,
                                                   report_period = data.ip_report.report_period.period,
                                                   call = data.ip_report.RFP.RFP_name,
                                                   year = data.ip_report.year.year1,
                                                   data.venue,
                                                   data.activity,
                                                   data.date
                                               };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource upcomingActivityDatasource = new ReportDataSource("UpcomingActivityDataSet", upcomingActivitiesReport);
                viewer.LocalReport.DataSources.Add(upcomingActivityDatasource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/UpcomingActivityReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in upcomingActivitiesReport)
                {
                    fileName = "Upcoming Activity Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
            else
            {
                var upcomingActivitiesReport = from data in db.upcoming_activities
                                               where data.RFP_id == call &&
                                               data.year_id == year &&
                                               data.report_period_id == reportPeriod
                                               select new
                                               {
                                                   ip_name = data.ip_report.implementing_partner.abbreviation,
                                                   report_period = data.ip_report.report_period.period,
                                                   call = data.ip_report.RFP.RFP_name,
                                                   year = data.ip_report.year.year1,
                                                   data.venue,
                                                   data.activity,
                                                   data.date
                                               };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource upcomingActivityDatasource = new ReportDataSource("UpcomingActivityDataSet", upcomingActivitiesReport);
                viewer.LocalReport.DataSources.Add(upcomingActivityDatasource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/UpcomingActivityReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in upcomingActivitiesReport)
                {
                    fileName = "Upcoming Activity Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
        }

        public void createRiskandMitigationReport(int call, int year, int reportPeriod)
        {
            if (reportPeriod == 5)
            {
                var riskAndMitigationReport = from data in db.risk_and_mitigation
                                              where data.RFP_id == call &&
                                              data.year_id == year
                                              select new
                                              {
                                                  ip_name = data.ip_report.implementing_partner.abbreviation,
                                                  report_period = data.ip_report.report_period.period,
                                                  year = data.ip_report.year.year1,
                                                  call = data.ip_report.RFP.RFP_name,
                                                  data.risk,
                                                  data.mitigation
                                              };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource riskAndMitigationDatasource = new ReportDataSource("RiskAndMitigationDataSet", riskAndMitigationReport);
                viewer.LocalReport.DataSources.Add(riskAndMitigationDatasource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/RiskAndMitigationReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in riskAndMitigationReport)
                {
                    fileName = "Upcoming Activity Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
            else
            {
                var riskAndMitigationReport = from data in db.risk_and_mitigation
                                              where data.RFP_id == call &&
                                              data.report_period_id == reportPeriod &&
                                              data.year_id == year
                                              select new
                                              {
                                                  ip_name = data.ip_report.implementing_partner.abbreviation,
                                                  report_period = data.ip_report.report_period.period,
                                                  year = data.ip_report.year.year1,
                                                  call = data.ip_report.RFP.RFP_name,
                                                  data.risk,
                                                  data.mitigation
                                              };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource riskAndMitigationDatasource = new ReportDataSource("RiskAndMitigationDataSet", riskAndMitigationReport);
                viewer.LocalReport.DataSources.Add(riskAndMitigationDatasource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/RiskAndMitigationReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in riskAndMitigationReport)
                {
                    fileName = "Upcoming Activity Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
        }

        public void createPhotographsReport(int call, int year, int reportPeriod)
        {
            if (reportPeriod == 5)
            {
                string path = Server.MapPath("~/Amkeni_System/ip_photos/");
                var ipPhotosReport = from data in db.ip_photo
                                     where data.year_id == year &&
                                     data.RFP_id == call
                                     select new
                                     {
                                         ip_name = data.ip_report.implementing_partner.abbreviation,
                                         report_period = data.ip_report.report_period.period,
                                         year = data.ip_report.year.year1,
                                         call = data.ip_report.RFP.RFP_name,
                                         data.photo_id,
                                         data.caption,
                                         name = "file:///" + path + data.name
                                     };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource ipPhotoDataSource = new ReportDataSource("IpPhotoDataSet", ipPhotosReport);
                viewer.LocalReport.DataSources.Add(ipPhotoDataSource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/IpPhotoReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in ipPhotosReport)
                {
                    fileName = "IP Photos Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
            else
            {
                string path = Server.MapPath("~/Amkeni_System/ip_photos/");
                var ipPhotosReport = from data in db.ip_photo
                                     where data.report_period_id == reportPeriod &&
                                     data.year_id == year &&
                                     data.RFP_id == call
                                     select new
                                     {
                                         ip_name = data.ip_report.implementing_partner.abbreviation,
                                         report_period = data.ip_report.report_period.period,
                                         year = data.ip_report.year.year1,
                                         call = data.ip_report.RFP.RFP_name,
                                         data.photo_id,
                                         data.caption,
                                         name = "file:///" + path + data.name
                                     };

                ReportViewer viewer = new ReportViewer();
                ReportDataSource ipPhotoDataSource = new ReportDataSource("IpPhotoDataSet", ipPhotosReport);
                viewer.LocalReport.DataSources.Add(ipPhotoDataSource);
                viewer.LocalReport.EnableExternalImages = true;
                viewer.LocalReport.ReportPath = "Reports/IpPhotoReport.rdlc";

                //Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
                string fileName = string.Empty;
                foreach (var metaData in ipPhotosReport)
                {
                    fileName = "IP Photos Report for " + metaData.ip_name;
                }

                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                //After getting the bytes representing the PDF report, buffer it and send it to the client.
                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AppendHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = mimeType;
                Response.AddHeader("content-disposition", "attachment:filename=" + fileName + "." + extension);
                Response.BinaryWrite(bytes); //Create the file
                Response.Flush(); //Send to client for download
            }
        }
    }
}